package com.fqr;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import com.cron.CronExpression;

public class CronTrigger extends FlexTrigger {
	
	private Logger logger = Logger.getLogger(CronTrigger.class);

	private String expression;
	private CronExpression cron;

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		logger.info(getName() + " has cron expression " + expression);
		this.expression = expression;
		this.cron = new CronExpression(expression);
		new Timer(false).schedule(new CronTimerTask(), 1000, 1000);
	}

	private class CronTimerTask extends TimerTask {
		public void run() {
			logger.debug(getName() + " timer fired");
			if (cron.on()) {
				logger.debug(getName() + " cron fired");
				TriggerEvent e = new TriggerEvent(CronTrigger.this);
				e.setEventDetail(expression);
				fire(e);
			}
		}
	}

}
